/**
 * Created by Demon-pro on 2015/6/28.
 */
var Calculate = function () {
    function buttonHandle(){
        $("#nextPage").on("click",function (){
            location.href = basePath + "mill/heat?menuId=38"
        })
    }

    function init() {
        Metronic.blockUI({
            boxed:true,
            message:"数据计算中..."
        });
        $.post(basePath + "mill/calculate/getCalculateInit",function(){}).done(
            $.get(basePath + "mill/calculate/getCalculate",function(data) {
                $("input").autoInject({
                    data:data,
                    mode:"id"
                },function($obj) {
                    if ($obj.val() == 0 || $obj.val() =="" || $obj == null) {
                        if ($obj.attr("id") == "E37") {
                            $obj.val("不合格");
                        }

                    }
                    $("#E").val(data.model)
                });
                Metronic.unblockUI();
                save()
            })
        ).done();
    }

    var change = function() {
        $("#E").on("change",function() {
            Metronic.blockUI({
                boxed:true,
                message:"数据计算中..."
            });
            $.get(basePath + "mill/calculate/millChange?model=" + $("#E").val(),function(data){
                $("input").autoInject({
                    data:data,
                    mode:"id"
                },function($obj) {
                    if ($obj.val() == 0 || $obj.val() =="" || $obj == null) {
                        if ($obj.attr("id") == "E37") {
                            $obj.val("不合格");
                        }

                    }
                });
                Metronic.unblockUI();
                save();
            }).done();
        });
    };

    var save = function() {
        $("input").each(function(index, element) {
            var id = $(this).attr("id");
            if (id != "menuParentId" || id != "menuId" ) {
                Calculate.update(id)
            }

        });
    };

    return {
        init: function () {
            init();
            change();
            buttonHandle();
        },
        update: function (ExParam) {
            if (ExParam.charAt(0) != "E") {
                return;
            }
            var save = function (exParam, exVal) {
                if (validate.isNotNumber(exVal)) {
                    validate.invalid();
                    return;
                }
                $.ajax({
                    url: basePath + "mill/calculate/saveValueOfSelectCalculate",
                    dataType: "json",
                    type: "post",
                    //放了三个值，一个填到默认Ex当中，其他两个分别对应属性值和属性名
                    data: exParam + "=" + exVal + "&paramName=" + exParam + "&paramValue=" + exVal
                });
                validate.valid();
            };
            var $obj = $("#" + ExParam);
            var ExVal = $obj.val();
            validate.init($obj);
            if (ExVal != "") {
                save(ExParam, ExVal);
            } else {
                //判断是否可以计算，如果可以后台计算后返回结果，保存结果，不可以计算提示
                if ($obj.hasClass("calculable")) {
                    $.ajax({
                        url: basePath + "admin/formal/calculate",
                        dataType: "json",
                        type: "post",
                        //燃烧前缀 R
                        data: "paramName=A" + ExParam,
                        success: function (data) {
                            $obj.val(data);
                            save(ExParam, data);
                        }
                    });
                    validate.valid();
                }
                else
                    validate.invalid();
            }
        }
    }



}();